package com.mall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.db.domain.Notice;
import com.mall.db.service.NoticeService;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 通知表 前端控制器
 * </p>
 *
 * @author zzz
 * @since 2021-09-26
 */
@Controller
@RequestMapping("/notice/nc")
public class NoticeController extends BaseController<Notice>{

    @Resource(name = "noticeService")
    private NoticeService ns;

    @Override

    public Map<String, Object> find(Notice notice) {

        //实现分页
        PageHelper.startPage(notice.getPage(),notice.getLimit());

        QueryWrapper<Notice> qw = new QueryWrapper<>();

        List<Notice> list = ns.list(qw);

        PageInfo<Notice> page = new PageInfo<>(list,notice.getLimit());


        return this.findMap(page.getList(),page.getTotal());
    }

    @Override
    public Map<String, Object> add(Notice notice) {
        boolean b = ns.save(notice);
        return this.iud("新增",b,notice);
    }

    @Override
    public Map<String, Object> update(Notice notice) {
        boolean b = ns.updateById(notice);
        return this.iud("编辑", b, notice);
    }

    @Override
    public Map<String, Object> delete(Notice notice) {
        Map<String, Object> map = new HashMap<>();
        if (!StringUtils.isEmpty(notice.getId())) {
            ns.removeById(notice.getId());
            map.put("is", true);
            map.put("msg", "删除成功!");
        } else {
            map.put("is", false);
            map.put("msg", "删除失败!");
        }
        return map;
    }

}

